package com.company.example.customElement;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 * Created by ywl on 2016/12/21.
 */
public class AccountBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{
    //Element 对应的类
    protected Class getBeanClass(Element element) {
        return Account.class;
    }

    //从Element 中解析并提取对应的元素
    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        String userName = element.getAttribute("userName");
        Integer age = Integer.valueOf(element.getAttribute("age"));
        //将提取的数据放入到 BeanDefinitionBuilder中，待到完成所有bean的解析后统一注册到 BeanFactory 中
        if(StringUtils.hasText(userName)) {
            builder.addPropertyValue("userName",userName);
        }
        if(age != null) {
            builder.addPropertyValue("age",age);
        }
    }
}
